package com.znsb.msfq.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.ImageView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;
import com.znsb.msfq.R;
import com.znsb.msfq.app.ZnsbApp;

import java.io.File;

/**
 * Created by Administrator on 2016/12/8.
 */

public class ImageLoader {

    /**
     * 有占位图
     *
     * @param imageView
     * @param url
     */
    public static void imageLoder(ImageView imageView, Object url) {
        if (!TextUtils.isEmpty("" + url)) {
            if (url instanceof File) {
                Picasso.with(imageView.getContext())
                        .load((File) url)
                        .memoryPolicy(MemoryPolicy.NO_CACHE)
                        .networkPolicy(NetworkPolicy.NO_CACHE)
                        .placeholder(R.mipmap.img_default)
                        .error(R.mipmap.img_default)
                        .into(imageView);
            } else if (url instanceof Uri) {
                Picasso.with(imageView.getContext())
                        .load((Uri) url)
                        .memoryPolicy(MemoryPolicy.NO_CACHE)
                        .networkPolicy(NetworkPolicy.NO_CACHE)
                        .placeholder(R.mipmap.img_default)
                        .error(R.mipmap.img_default)
                        .into(imageView);
            } else if (url instanceof Integer) {
                Picasso.with(imageView.getContext())
                        .load((Integer) url)
                        .memoryPolicy(MemoryPolicy.NO_CACHE)
                        .networkPolicy(NetworkPolicy.NO_CACHE)
                        .placeholder(R.mipmap.img_default)
                        .error(R.mipmap.img_default)
                        .into(imageView);
            } else {
                Picasso.with(imageView.getContext())
                        .load((String) url)
                        .memoryPolicy(MemoryPolicy.NO_CACHE)
                        .networkPolicy(NetworkPolicy.NO_CACHE)
                        .placeholder(R.mipmap.img_default)
                        .error(R.mipmap.img_default)
                        .into(imageView);
            }
        }

    }

    /**
     * 加载图片
     *
     * @param imageView
     * @param url
     */
    public static void imageLoderNo(ImageView imageView, String url) {
        if (!TextUtils.isEmpty("" + url))
            Picasso.with(imageView.getContext()).load(url)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .networkPolicy(NetworkPolicy.NO_CACHE)
                    .into(imageView);
    }

    public static void imageCircleLoder(ImageView imageView, Object url) {
        if (!TextUtils.isEmpty("" + url)) {
            if (url instanceof File) {
                Picasso.with(imageView.getContext())
                        .load((File) url)
                        .memoryPolicy(MemoryPolicy.NO_CACHE)
                        .networkPolicy(NetworkPolicy.NO_CACHE)
                        .transform(new CircleImageTransformation())
                        .placeholder(R.mipmap.img_default)
                        .error(R.mipmap.img_default)
                        .into(imageView);
            } else if (url instanceof Uri) {
                Picasso.with(imageView.getContext())
                        .load((Uri) url)
                        .memoryPolicy(MemoryPolicy.NO_CACHE)
                        .networkPolicy(NetworkPolicy.NO_CACHE)
                        .transform(new CircleImageTransformation())
                        .placeholder(R.mipmap.img_default)
                        .error(R.mipmap.img_default)
                        .into(imageView);
            } else if (url instanceof Integer) {
                Picasso.with(imageView.getContext())
                        .load((Integer) url)
                        .memoryPolicy(MemoryPolicy.NO_CACHE)
                        .networkPolicy(NetworkPolicy.NO_CACHE)
                        .transform(new CircleImageTransformation())
                        .placeholder(R.mipmap.img_default)
                        .error(R.mipmap.img_default)
                        .into(imageView);
            } else {
                Picasso.with(imageView.getContext())
                        .load((String) url)
                        .memoryPolicy(MemoryPolicy.NO_CACHE)
                        .networkPolicy(NetworkPolicy.NO_CACHE)
                        .transform(new CircleImageTransformation())
                        .placeholder(R.mipmap.img_default)
                        .error(R.mipmap.img_default)
                        .into(imageView);
            }
        }

    }

    public static class CircleImageTransformation implements Transformation {

        /**
         * A unique key for the transformation, used for caching purposes.
         */
        private static final String KEY = "circleImageTransformation";

        @Override
        public Bitmap transform(Bitmap source) {

            int minEdge = Math.min(source.getWidth(), source.getHeight());
            int dx = (source.getWidth() - minEdge) / 2;
            int dy = (source.getHeight() - minEdge) / 2;

            // Init shader
            Shader shader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            Matrix matrix = new Matrix();
            matrix.setTranslate(-dx, -dy);   // Move the target area to center of the source bitmap
            shader.setLocalMatrix(matrix);

            // Init paint
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setShader(shader);

            // Create and draw circle bitmap
            Bitmap output = Bitmap.createBitmap(minEdge, minEdge, source.getConfig());
            Canvas canvas = new Canvas(output);
            canvas.drawOval(new RectF(0, 0, minEdge, minEdge), paint);

            // Recycle the source bitmap, because we already generate recyler_divider new one
            source.recycle();

            return output;
        }

        @Override
        public String key() {
            return KEY;
        }
    }
}
